package cn.stronglink.assetmanage.business.check.assetlist;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.detail.CheckDetailActivity;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.MessageEvent;
import cn.stronglink.assetmanage.listener.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.dao.CheckDataUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class CheckAssetFragment extends Fragment implements onRecyclerListener {
    private String orderNo, location;
    private Unbinder mUnbinder;
    private List<Asset> mAssetList = new ArrayList<>();
    @BindView(R.id.rv_check_asset_list)
    RecyclerView rvCheckAssetList;
    @BindView(R.id.cb_check)
    CheckBox cb_check;
    private CheckAssetAdapter mAdapter;
    private AssetListActivity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (AssetListActivity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check_asset, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cb_check.setVisibility(View.GONE);
        mAdapter = new CheckAssetAdapter(mActivity);
        mAdapter.setListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity,
                LinearLayoutManager.VERTICAL, false);
        rvCheckAssetList.setLayoutManager(layoutManager);
        rvCheckAssetList.setAdapter(mAdapter);
        setOrderInfo(AssetListActivity.orderNo, mActivity.getString(R.string.all_location));
    }

    public void setOrderInfo(final String orderNo, final String location) {
        this.orderNo = orderNo;
        this.location = location;
        new Thread(new Runnable() {
            @Override
            public void run() {
                mAssetList = CheckDataUtil.getCheckAssetByOrderNoAndLocation(orderNo, location);
                if (mActivity!=null){
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mAdapter != null) {
                                mAdapter.notifyDataChanged(mAssetList);
                            }
                        }
                    });
                }
            }
        }).start();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onItemClicked(View v, int postion) {
        // 跳转到详情页
        if (mActivity != null) {
            Intent intent = new Intent(mActivity, CheckDetailActivity.class);
            intent.putExtra(Constants.CHECK_IN_DETAIL_NO, mAssetList.get(postion).getAsset_id_key());
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateData(MessageEvent messageEvent) {
        if (messageEvent != null && Constants.FIFTLER_ASSET_DATA.equals(messageEvent.getType())) {
            String[] dataArr = (String[]) messageEvent.getMessage();
            String orderNo = dataArr[0];
            String roomName = dataArr[1];
            setOrderInfo(orderNo, roomName);
        }
    }
}
